<template>
    <div class="">
      <!-- 表单 -->
      <el-form ref="form" :model="form" :inline="true" label-width="80px">
        <el-form-item label="图书信息">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search()">查询</el-button>
          <el-button type="primary" @click="dialogFormVisible = true"
            >添加图书</el-button
          >
        </el-form-item>
      </el-form>

      <el-dialog
        title="添加图书"
        :inline="true"
        center
        :visible.sync="dialogFormVisible"
      >
        <el-form :model="ruleForm">
          <el-form-item label="图书号" size="mini">
            <el-input v-model="ruleForm.card"></el-input>
          </el-form-item>
          <el-form-item label="图书名称" size="mini">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="图书类型" size="mini">
            <el-input v-model="ruleForm.type"></el-input>
          </el-form-item>
          <el-form-item label="作者名称" size="mini">
            <el-input v-model="ruleForm.autho"></el-input>
          </el-form-item>
          <el-form-item label="出版社" size="mini">
            <el-input v-model="ruleForm.press"></el-input>
          </el-form-item>
          <el-form-item label="总数量" size="mini">
            <el-input v-model="ruleForm.num"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addBook()">确 定</el-button>

        </div>
      </el-dialog>

      <el-dialog
        title="修改图书"
        :inline="true"
        center
        :visible.sync="dialogFormVisible1"
      >
        <el-form :model="ruleForm">
          <el-form-item label="图书号" size="mini">
            <el-input v-model="ruleForm.card" placeholder=""></el-input>
          </el-form-item>
          <el-form-item label="图书名称" size="mini">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="图书类型" size="mini">
            <el-input v-model="ruleForm.type"></el-input>
          </el-form-item>
          <el-form-item label="作者名称" size="mini">
            <el-input v-model="ruleForm.autho"></el-input>
          </el-form-item>
          <el-form-item label="出版社" size="mini">
            <el-input v-model="ruleForm.press"></el-input>
          </el-form-item>
          <el-form-item label="总数量" size="mini">
            <el-input v-model="ruleForm.num"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible1 = false">取 消</el-button>
          <el-button type="primary" @click="addBook()">确 定</el-button>

        </div>
      </el-dialog>

      <!-- 表格 -->
      <el-table :data="tableData.list" border style="width:100%">

        <el-table-column
          prop="card"
          label="图书号"
          align="center"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="type"
          label="图书类型"
          width="150"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="图书名称"
          align="center"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="autho"
          label="作者名称"
          width="150"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="press"
          label="出版社"
          width="150"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="num"
          label="总数量"
          width="150"
          align="center"
        ></el-table-column>


        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.bid)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div style="margin-top:30px;text-align:center">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="tableData.total"
          :page-size="tableData.pageSize"
          :current-page="tableData.pageNum"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>

      <el-dialog
        title="提示"
        :visible.sync="centerDialogVisible"
        width="30%"
        center
      >
        <span>数据删除不可恢复，确认是否删除？</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="deleteUser()">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </template>

  <script>
  export default {
    components: {},
    data() {
      return {
        form: {
          name: ""
        },
        ids: -1, //删除的id
        ruleForm: {
          name: "",
          password: "",
          age: "",
          address: "",
          birthday: "",
          phone: ""
        },
        tableData: [],
        currentPage: 1, //当前页
        dialogFormVisible: false, //新增用户弹框
        centerDialogVisible: false, //删除弹框
        dialogFormVisible1: false, //新增用户弹框
        centerDialogVisible1: false //删除弹框



      };
    },
    //vue生命周期函数,页面加载时进行触发
    created() {
      this.finaAll();
    },
    mounted() {},
    watch: {},
    computed: {},
    methods: {
      //模糊查询
      search() {
        this.finaAll();
      },

      handleDelete(bid) {
        this.ids = bid;
        this.centerDialogVisible = true;
      },

      //点击触发分页
      handleCurrentChange(val) {
        this.currentPage = val;
        this.finaAll();
      },

      deleteUser() {
        console.log("=====================================");
        console.log(this.ids);
        this.$http
          .get("http://localhost:8090/deleteBook?bid=" + this.ids)
          .then(result => {
            if (result.data.code == 1005) {
              this.$message({
                message: result.data.msg,
                type: "success"
              });
              this.finaAll(); // 刷新。页面
            }
          })
          .catch(err => {});
        this.centerDialogVisible = false;
      },

      //增加用户
      addBook() {
        //前端怎么传递对象到后端
        let params = new URLSearchParams();
        params.append("card", this.ruleForm.card);
        params.append("type", this.ruleForm.type);
        params.append("name", this.ruleForm.name);
        params.append("autho", this.ruleForm.autho);
        params.append("press", this.ruleForm.press);
        params.append("num", this.ruleForm.num);
  console.log(params);
        this.$http
          .post("http://localhost:8090/addBook", params)
          .then(result => {
            if (result.data.code == 2000) {
              this.$message({
                message: result.data.msg,
                type: "success"
              });
              this.finaAll(); // 刷新页面
            }
          })
          .catch(err => {});
        this.dialogFormVisible = false;
      },

      //查询所有的方法
      finaAll() {
        this.$http
          .get("http://localhost:8090/bookList", {
            params: {
              name: this.form.name,
              pageNum: this.currentPage
            }
          })
          .then(result => {
            console.log(result);
            this.tableData = result.data.data;
          })
          .catch(err => {});
      }
    }
  };
  </script>
  <style scoped></style>
